//  BackgroundView.m
//  BrianSux
//
//  Created by Lion User on 16/06/2012.
//  Copyright (c) 2012 Stony Brook University Suny. All rights reserved.
//

#import "GameView.h"

@implementation GameView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

- (void)refresh:(CGRect) rectTop: (CGRect)rectBottom: (CGRect)rectBall: (int[480][2]) backgroundPoints: (BOOL)stopFlying;
{
    stopFly=stopFlying;
    ballRect = rectBall;
    topRect= rectTop;
    bottomRect = rectBottom;
    for (int x=0;x<=480;x++){
        for (int y=0; y<2; y++){
            points[x][y]=backgroundPoints[x][y];
        }
    }    
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    
    
    //CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    // Initialization imagestep
    if (imagestep == 0) {
        imagestep = 1;
    }
    //NSLog(@"%d",stopFly);
    UIImage *image;
    //if (!stopFly){
        if (imagestep == 1) {
            image = [UIImage imageNamed:@"bee1.png"];
            imagestep = 2;
        } 
        else if(imagestep == 2){
            image = [UIImage imageNamed:@"bee1.png"];
            imagestep = 3;
        }
        else if(imagestep == 3){
            image = [UIImage imageNamed:@"bee2.png"];
            imagestep = 4;} 
        else if(imagestep == 4){
            image = [UIImage imageNamed:@"bee2.png"];
            imagestep = 1;}
        
    //}
    //else{
    //    image=([UIImage imageNamed:@"bee1.png"]);
    //}
    [image drawInRect:ballRect];
    
    UIImage *topBackground;
    topBackground = [UIImage imageNamed:@"object1.png"];
    CGPoint newPoint = CGPointMake(topRect.origin.x, topRect.origin.y);
    [topBackground drawAtPoint: newPoint];
    UIImage *topBackground2;
    topBackground2 = [UIImage imageNamed:@"object2.png"];
    CGPoint newPoint2 = CGPointMake(topRect.origin.x+960, topRect.origin.y);
    [topBackground2 drawAtPoint: newPoint2];    
    
    /*
    CGContextSetRGBFillColor(contextRef, 255, 0, 0, 1);    CGContextSetStrokeColorWithColor(contextRef, [UIColor yellowColor].CGColor);
    CGContextSetLineWidth(contextRef, 1.0);
    CGContextAddEllipseInRect(contextRef, topRect);
    CGContextFillEllipseInRect(contextRef, topRect);    
    CGContextStrokePath(contextRef);  
    
    CGContextSetStrokeColorWithColor(contextRef, [UIColor yellowColor].CGColor);
    CGContextSetLineWidth(contextRef, 1.0);
    CGContextAddEllipseInRect(contextRef, bottomRect);
    CGContextFillEllipseInRect(contextRef, bottomRect);    
    CGContextStrokePath(contextRef);    
    */
}

@end